package org.rainbowinverted.charging.device.connect.ykc.server.util;

import org.rainbowinverted.charging.device.api.operation.constants.DeviceOpConstantEnum;
import org.rainbowinverted.charging.device.connect.ykc.server.constants.YkcOpCodeConstantsEnum;
import org.rainbowinverted.charging.device.connect.ykc.server.message.out.YkcMessageOut;
import org.rainbowinverted.charging.device.connect.ykc.server.handler.*;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hehuaiping
 */
@Component
public class YkcOpCodeHandlerMap {
    /**
     * 平台操作码转换为设备操作码
     */
    private static final Map<Integer, Integer> OP_CODE_MAP = new HashMap<>(8);

    /**
     * 读写处理程序映射
     */
    private final static Map<Integer, YkcAbstractHandler<?, ? extends YkcMessageOut>> READ_WRITE_HANDLER_MAP =
            new HashMap<>(8);

    public YkcOpCodeHandlerMap(YkcDeviceLoginHandler ykcDeviceLoginHandler,
                               YkcDeviceHeartbeatHandler ykcDeviceHeartbeatHandler,
                               YkcChargeStandardCheckHandler chargeStandardCheckHandler,
                               YkcTimeSyncHandler ykcTimeSyncHandler,
                               YkcChargeStandardSyncHandler ykcChargeStandardSyncHandler,
                               YkcChargeStandardSettingHandler ykcChargeStandardSettingHandler,
                               YkcDeviceGunStatusHandler ykcDeviceGunStatusHandler,
                               YkcDeviceStartChargeHandler ykcDeviceStartChargeHandler,
                               YkcDeviceStopChargeHandler ykcDeviceStopChargeHandler,
                               YkcDeviceChargeRecordHandler ykcDeviceChargeRecordHandler,
                               YkcDeviceDisconnectHandler ykcDeviceDisconnectHandler,
                               YkcDeviceQrcodeHandler ykcDeviceQrcodeHandler) {
        OP_CODE_MAP.put(DeviceOpConstantEnum.CONNECT.getOpCode(), 0x01);
        OP_CODE_MAP.put(DeviceOpConstantEnum.CONNECT_RESP.getOpCode(), 0x02);
        OP_CODE_MAP.put(DeviceOpConstantEnum.HEARTBEAT.getOpCode(), 0x03);
        OP_CODE_MAP.put(DeviceOpConstantEnum.HEARTBEAT_RESP.getOpCode(), 0x04);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_CHECK.getOpCode(), 0x05);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_CHECK_RESP.getOpCode(), 0x06);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SYNC.getOpCode(), 0x09);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SYNC_RESP.getOpCode(), 0x0a);
        OP_CODE_MAP.put(DeviceOpConstantEnum.DEVICE_STATUS_REPORT.getOpCode(), 0x12);
        OP_CODE_MAP.put(DeviceOpConstantEnum.DEVICE_STATUS_REPORT_RESP.getOpCode(), 0x13);
        OP_CODE_MAP.put(DeviceOpConstantEnum.START_CHARGE.getOpCode(), 0x34);
        OP_CODE_MAP.put(DeviceOpConstantEnum.START_CHARGE_RESP.getOpCode(), 0x33);
        OP_CODE_MAP.put(DeviceOpConstantEnum.STOP_CHARGE.getOpCode(), 0x36);
        OP_CODE_MAP.put(DeviceOpConstantEnum.STOP_CHARGE_RESP.getOpCode(), 0x35);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_RECORD.getOpCode(), 0x3b);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_RECORD_RESP.getOpCode(), 0x40);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.DEVICE_TIME_SYNC.getOpCode(), 0x56);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.DEVICE_TIME_SYNC.getOpCode(), 0x56);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.DEVICE_TIME_SYNC_RESP.getOpCode(), 0x55);
        OP_CODE_MAP.put(DeviceOpConstantEnum.UPDATE_DEVICE_QR.getOpCode(), 0xf1);
        OP_CODE_MAP.put(DeviceOpConstantEnum.UPDATE_DEVICE_QR_RESP.getOpCode(), 0xf0);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SETTING.getOpCode(), 0x58);
        OP_CODE_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SETTING_RESP.getOpCode(), 0x57);



        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.CONNECT.getOpCode(), ykcDeviceLoginHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.CONNECT_RESP.getOpCode(), ykcDeviceLoginHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.HEARTBEAT.getOpCode(), ykcDeviceHeartbeatHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.HEARTBEAT_RESP.getOpCode(), ykcDeviceHeartbeatHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_CHECK.getOpCode(), chargeStandardCheckHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_CHECK_RESP.getOpCode(), chargeStandardCheckHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SYNC.getOpCode(), ykcChargeStandardSyncHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SYNC_RESP.getOpCode(), ykcChargeStandardSyncHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.DEVICE_TIME_SYNC.getOpCode(), ykcTimeSyncHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.DEVICE_TIME_SYNC_RESP.getOpCode(), ykcTimeSyncHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.DEVICE_STATUS_REPORT.getOpCode(), ykcDeviceGunStatusHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.DEVICE_STATUS_REPORT_RESP.getOpCode(), ykcDeviceGunStatusHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.START_CHARGE.getOpCode(), ykcDeviceStartChargeHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.START_CHARGE_RESP.getOpCode(), ykcDeviceStartChargeHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.STOP_CHARGE.getOpCode(), ykcDeviceStopChargeHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.STOP_CHARGE_RESP.getOpCode(), ykcDeviceStopChargeHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_RECORD.getOpCode(), ykcDeviceChargeRecordHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_RECORD_RESP.getOpCode(), ykcDeviceChargeRecordHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.DISCONNECT.getOpCode(), ykcDeviceDisconnectHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.UPDATE_DEVICE_QR.getOpCode(), ykcDeviceQrcodeHandler);
        READ_WRITE_HANDLER_MAP.put(DeviceOpConstantEnum.UPDATE_DEVICE_QR_RESP.getOpCode(), ykcDeviceQrcodeHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SETTING.getOpCode(), ykcChargeStandardSettingHandler);
        READ_WRITE_HANDLER_MAP.put(YkcOpCodeConstantsEnum.CHARGE_STANDARD_SETTING_RESP.getOpCode(), ykcChargeStandardSettingHandler);

    }

    /**
     * 获取handler
     * @param platformOpCode platformOpCode
     * @return {@link YkcAbstractHandler}<{@link ?},{@link YkcMessageOut}>
     */
    public YkcAbstractHandler<?,YkcMessageOut> getHandler(Integer platformOpCode) {
        return (YkcAbstractHandler<?, YkcMessageOut>) READ_WRITE_HANDLER_MAP.get(platformOpCode);
    }

    /**
     * 设备操作码转换成平台操作码
     * @param deviceOpCode 设备操作码
     * @return {@link Integer}
     */
    public Integer convertToPlatformCode(Integer deviceOpCode) {
        for (Map.Entry<Integer, Integer> entry : OP_CODE_MAP.entrySet()) {
            if (entry.getValue().equals(deviceOpCode)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * 平台操作码转换成设备操作码
     * @param platformOpCode 平台操作码
     * @return {@link Integer}
     */
    public Integer convertToDeviceCode(Integer platformOpCode) {
        return OP_CODE_MAP.get(platformOpCode);
    }
}
